<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/checkLogin.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>

    <script>
        $(function () {
            var params = getParams();   // 获取id

            // 给表单设置action(默认新增)
            $("#detailForm").attr("action", baseUrl + "/togetherTrips");

            // 查询全部地区
            $.ajax({
                url: baseUrl + "/regions",
                method: "get",
                success: function (data) {
                    var temp = "";
                    $.each(data, function (index, ele) {
                        temp += "<option value='" + ele.id + "'>" + ele.name + "</option>";

                    });
                    $("#regionSelect").html(temp);

                }
            });

            // 利用文件框的值改变事件
            $("#fileInput").change(function () {
                console.log($(this).val());
                if ($(this).val()) { // 有值才提交
                    // 浏览器获得的value值是一个fake值，假的路径，所以需要
                    // 利用表单来提交，表单中是真实的路径。
                    $("#fileForm").ajaxSubmit({
                        url: baseUrl + "/images",
                        success: function (data) {
                            if (data.status == 1) {
                                $("#coverUrl").attr('src', data.url);
                            }
                        }
                    });
                }
            });

            // 点击更换封面
            $("#changeBtn").click(function () {
                $("#fileInput").click();
            });

            // 富文本编辑器
            var Edr = new Eleditor({
                el: "#contentEditor",   // 要使用富文本编辑器的位置
                upload: {
                    server: baseUrl + "/images"    // 文件上传的接口路径
                }
            });

            // 将表单变成ajax表单
            $("#detailForm").ajaxForm({
                beforeSubmit: function (arr) {
                    // 作用2: 在提交表单前添加额外的参数
                    arr.push({name: "title", value: $("#title").val()});
                    arr.push({name: "coverUrl", value: $("#coverUrl").attr("src")});
                    arr.push({name: "content.content", value: Edr.getContent()});
                    arr.push({name: "state", value: true});
                    arr.push({name: "author.id", value: user.id});
                },
                success: function (data) {
                    window.location.href = "/together.html";
                }
            });



            $("#release").click(function () {
                $("#detailForm").submit();
            });
        });

    </script>

</head>

<body>
<form id="fileForm" method="post">
    <input type="file" name="file" id="fileInput" style="display: none">
</form>

<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>

        <button type="button" class="btn" id="changeBtn">
            <i class="fa fa-pencil-square-o"></i>
            <small>选择封面</small>
        </button>


    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" />
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <div id="contentEditor" render-html="content.content" >
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post">
            <label>出行时间:</label>
            <input type="text" name="goTime" class="form-control" onclick="WdatePicker()">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control">
            <label>旅游消费等级:</label>
            <select class="c-select form-control" name="consumeLevel">
                <option value="1" selected>普通</option>
                <option value="0">穷游</option>
                <option value="2">中等</option>
                <option value="3">高级</option>
            </select>
            <label>旅游人数:</label>
            <input type="text" name="person" class="form-control" >

            <label>旅游地点:</label>
            <select class="form-control" id="regionSelect" name="region.id" >
            </select>
            <label>结伴简介:</label>
            <textarea  cols="66" rows="5" name="intro">
            </textarea>

        </form>
    </div>
    <hr/>
</div>
</body>
</html>